package com.ceb.proxy;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @author name:dss
 * @version 1.0
 * @description: TODO
 * @date 2022/8/232022/8/23
 */
@Aspect
@Component
public class LogProxy {

    @Pointcut("execution(* com.ceb.service.UserserviceImpl.*(..))")
    private void pointCut(){

    }
    @Before("pointCut()")
    public  void before(JoinPoint joinPoint){
        System.out.println("【前置通知】执行"+joinPoint.getSignature().getName()+"方法开始");
        System.out.println("【前置通知】所有参数为:" +Arrays.toString(joinPoint.getArgs()));
    }
    //@AfterReturning(value="execution(* com.ceb.service.UserserviceImpl.*(..))",returning = "res")
    @AfterReturning(value="pointCut()",returning = "res")
    public void afterReturning(JoinPoint joinPoint,Object res){
        System.out.println("【后置通知】执行"+ joinPoint.getSignature().getName() +"方法结束，返回结果为"+res);
    }
    //@Around("execution(* com.ceb.service.UserserviceImpl.add(..))")
    @Around("pointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("【环绕通知】执行"+ joinPoint.getSignature().getName() +"方法开始");
        long starttime=System.currentTimeMillis();

        Object [] args=joinPoint.getArgs();
        Object res =joinPoint.proceed(args);

        long endtime=System.currentTimeMillis();
        System.out.println("【环绕通知】执行"+ joinPoint.getSignature().getName() +"方法结束，用时"+(endtime-starttime)+"ms");

        if(res!=null) {
            return res.toString().toUpperCase();
        }
        return null;
    }

    @After("execution(* com.ceb.service.UserserviceImpl.*(..))")
    public  void after(JoinPoint joinPoint){
        System.out.println("【最终通知】----结束----");
    }
}
